﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace DAL
{
    using DAL.Entities;

    public class DoanhNghiepDAL : BaseDAL, UTL.IBaseDAL 
    {
        public int Count()
        {
            throw new NotImplementedException();
        }

        public DataTable Select()
        {
            string sql = @" select * from dn a join LINHVUC b on a.MSLV=b.MSLV ";
            return ExecuteQuery(sql);
        }

        public DataTable Select(object obj)
        {
            throw new NotImplementedException();
        }

        public object GetByKey(object key)
        {
            throw new NotImplementedException();
        }

        public bool Delete(string id)
        {
            string sql = @"DELETE FROM dn WHERE msdn = '{0}'";
            sql = string.Format(sql,id);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public bool Insert(object obj)
        {
            var o = (doanhnghiep)obj;
            string sql = @"set dateformat mdy; INSERT INTO dn(msdn,tendn,dc,dt,fax,mslv) VALUES('{0}',N'{1}',N'{2}','{3}','{4}','{5}') ";
            sql = string.Format(sql,o.madn,o.tendn,o.diachi,o.dienthoai,o.fax,o.malv);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public bool Update(object obj)
        {
            var o = (doanhnghiep)obj;
            string sql = @"set dateformat mdy; UPDATE dn SET tendn=N'{0}',dc=N'{1}',dt='{2}',fax='{3}',mslv='{4}' WHERE msdn='{5}' ";
            sql = string.Format(sql, o.tendn, o.diachi, o.dienthoai, o.fax, o.malv,o.madn);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public DataTable Search(string name)
        {
            throw new NotImplementedException();
        }


        /// <summary>
        /// lay ...
        /// </summary>
        /// <returns></returns>
        public string Select_max()
        {
            string sql = @"SELECT max(msdn) as id FROM dn";
            DataTable tb= ExecuteQuery(sql);
            
            return tb.Rows.Count>0? tb.Rows[0]["id"]+"":"0";
        }

        public DataTable select_linhvuc()
        {
            string sql = @"select mslv as id,tenlv as ten from linhvuc";
            return ExecuteQuery(sql);
        }

        public bool deleteobj(object obj)
        {
            throw new NotImplementedException();
        }
    }
}
